package parser

import (
	"github.com/xuxusheng/crawler/engine"
	"regexp"
	"strconv"
	"github.com/xuxusheng/crawler/model"
	"fmt"
)

var nameRe = regexp.MustCompile(
	`<a class="name fs24">(([^<]+))</a>`)

var genderRe = regexp.MustCompile(
	`<td><span class="label">性别：</span><span field="">([^<]+)</span></td>`)

var ageRe = regexp.MustCompile(
	`<td><span class="label">年龄：</span>([\d]+)岁</td>`)

var heightRe = regexp.MustCompile(
	`<td><span class="label">身高：</span><span field="">([^<]+)</span></td>`)

var weightRe = regexp.MustCompile(
	`<td><span class="label">体重：</span><span field="">([^<]+)</span></td>`)

var incomeRe = regexp.MustCompile(`<td><span class="label">月收入：</span>([^<]+)</td>`)

var marriageRe = regexp.MustCompile(
	`<td><span class="label">婚况：</span>([^<]+)</td>`)

var educationRe = regexp.MustCompile(`<td><span class="label">学历：</span>([^<]+)</td>`)

var occupationRe = regexp.MustCompile(`<td><span class="label">职业： </span>([^<]+)</td>`)

var hukouRe = regexp.MustCompile(
	`<td><span class="label">籍贯：</span>([^<]+)</td>`)

var xingzuoRe = regexp.MustCompile(
	`<td><span class="label">星座：</span><span field="">([^<]+)</span></td>`)

var houseRe = regexp.MustCompile(
	`<td><span class="label">住房条件：</span><span field="">([^<]+)</span></td>`)

var carRe = regexp.MustCompile(
	`<td><span class="label">是否购车：</span><span field="">([^<]+)</span></td>`)

func ParseProfile(contents []byte, name string) engine.ParseResult {

	profile := model.Profile{}

	profile.Name = name
	//profile.Name = extractString(contents, nameRe)

	profile.Gender = extractString(contents, genderRe)

	age, err := strconv.Atoi(
		extractString(contents, ageRe))
	if err == nil {
		profile.Age = age
	} else {
		fmt.Printf("extract age error: %v", err)
	}

	profile.Height = extractString(contents, heightRe)

	profile.Weight = extractString(contents, weightRe)

	profile.Income = extractString(contents, incomeRe)

	profile.Marriage = extractString(contents, marriageRe)

	profile.Education = extractString(contents, educationRe)

	profile.Occupation = extractString(contents, occupationRe)

	profile.Hukou = extractString(contents, hukouRe)

	profile.XingZuo = extractString(contents, xingzuoRe)

	profile.House = extractString(contents, houseRe)

	profile.Car = extractString(contents, carRe)

	result := engine.ParseResult{
		Items: []interface{}{profile},
	}

	return result

}

func extractString(contents []byte, re *regexp.Regexp) string {
	match := re.FindSubmatch(contents)
	if len(match) >= 2 {
		return string(match[1])
	} else {
		return ""
	}
}
